<!--
* description: 良品不良品转换-新增编辑弹窗
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="400px"
      @close="sendCode"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div
        class="filter-container filter-params"
        v-loading="loading"
        element-loading-text="正在加载源库位"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        style="overflow:hidden;"
      >
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :options="comp.options"
            :dateOptionsType="comp.dateOptionsType"
            :isInput="comp.isInput"
            :WAREHOUSE_ID="formField.WAREHOUSE_ID"
          ></component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import { wmsApis } from "@/api/wms/onStock/api";
import storage from "@/utils/storage";
import request from "@/utils/request";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  watch: {
    popupsVisible(val) {
      if (val) {
        if (
          this.formField.pageId !== "" &&
          this.$route.query.pageId !== undefined &&
          this.$route.query.pageId !== this.formField.pageId
        ) {
          this.formField.pageId = this.$route.query.pageId;
          this.formField.pageName = this.$route.query.pageName;
        }
        this.formField.langLabelName = this.formField.label;
        this.backFormField = JSON.parse(JSON.stringify(this.formField));
      }
    }
  },
  data() {
    return {
      // 保存API配置对象
      apiConfig: wmsApis.insertTransfrom,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存" //"保存"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "edit_compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_ID",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: [],
          isRequire: true,
          changed: val => {
            this.formField.PART_NO = "";
            this.formField.PART_NAME = "";
            this.querySPlace();
            this.queryTPlace();
          }
        },
        {
          compKey: "edit_compKey2",
          labelName: "备件名称",
          textField: "PART_NAME",
          codeField: "PART_NO",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsPartInfoStorage"),
          type: "propus",
          isMust: true,
          span: 23,
          isMul: false,
          isRequire: true,
          readonly: true,
          returnCodeField: "PART_NO",
          returnTextField: "PART_NAME",
          changed: val => {
            this.querySPlace();
          }
        },
        {
          compKey: "edit_compKey3",
          labelName: "转换数量",
          span: 23,
          codeField: "TRANSFORM_QTY",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          isRequire: true,
          isInput: true,
          changed: val => {
            if (!val) {
              this.$refs["trueedit_compKey3"][0].modelCode = "";
              this.formField.TRANSFORM_QTY = "";
              return;
            }
            this.$refs["trueedit_compKey3"][0].modelCode = (
              this.formField.TRANSFORM_QTY + ""
            ).replace(/[^\d^]+/g, "");
            this.formField.TRANSFORM_QTY = (
              this.formField.TRANSFORM_QTY + ""
            ).replace(/[^\d^]+/g, "");
          }
        },
        {
          compKey: "edit_compKey4",
          labelName: "转换类型",
          codeField: "TRANSFORM_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: [],
          isRequire: true,
          changed: val => {
            this.querySPlace();
            this.queryTPlace();
          }
        },
        {
          compKey: "edit_compKey5",
          labelName: "源库位",
          codeField: "S_PLACE_CODE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: [],
          isRequire: true
        },
        {
          compKey: "edit_compKey6",
          labelName: "目标库位",
          span: 23,
          codeField: "T_PLACE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          isRequire: true
        },
        {
          compKey: "edit_compKey7",
          labelName: "转换原因",
          span: 23,
          codeField: "TRANSFORM_REMARK",
          component: () => import("@/components/wms/textareaInput"),
          isMust: true
        }
      ],
      // 标题
      textMap: {
        add: "新增良品不良品转换"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        WAREHOUSE_ID: "",
        PART_NO: "",
        PART_NAME: "",
        TRANSFORM_QTY: "",
        TRANSFORM_TYPE: "",
        S_PLACE_CODE: "",
        T_PLACE_CODE: "",
        TRANSFORM_REMARK: ""
      },
      loading: false
    };
  },
  methods: {
    reset() {
      this.$utils.deepClone(this.formField, this.backFormField);
      this.tableComponents[4].options = [];
    },
    queryTPlace() {
      if (
        this.formField.TRANSFORM_TYPE === "01" &&
        this.formField.WAREHOUSE_ID
      ) {
        this.formField.T_PLACE_CODE = "";
        request({
          url: "/mp/wm/vl/warehouse/transfrom/queryNewPlaceBox.do",
          method: "post",
          data: {
            WAREHOUSE_ID: this.formField.WAREHOUSE_ID
          }
        }).then(res => {
          if (res.listRow && res.listRow.length > 0) {
            this.formField.T_PLACE_CODE = res.listRow[0].PLACE_CODE;
          } else {
            this.$message({
              message: res.debugMsg ? res.debugMsg : res.msg,
              type: "success"
            });
          }
        });
      }
    },
    querySPlace() {
      this.tableComponents[4].options = [];
      this.formField.S_PLACE_CODE = "";
      if (
        this.formField.WAREHOUSE_ID &&
        this.formField.PART_NO &&
        this.formField.TRANSFORM_TYPE
      ) {
        this.loading = true;
        request({
          url: "/mp/wm/vl/warehouse/transfrom/queryOldPlaceBox.do",
          method: "post",
          data: {
            WAREHOUSE_ID: this.formField.WAREHOUSE_ID,
            PART_NO: this.formField.PART_NO,
            TRANSFORM_TYPE: this.formField.TRANSFORM_TYPE
          }
        })
          .then(res => {
            this.loading = false;
            if (
              res.listResult &&
              res.listResult.rows &&
              res.listResult.rows.length > 0
            ) {
              res.listResult.rows.map(item => {
                this.tableComponents[4].options.push({
                  label: item.S_PLACE_CODE,
                  value: item.S_PLACE_CODE
                });
              });
            } else {
              this.$message({
                message: `该仓库下的${
                  this.formField.TRANSFORM_TYPE === "01" ? "良品" : "不良品"
                }备件没有库存`,
                type: "success"
              });
            }
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    onCreated() {
      if (this.curPopupsVisible) {
        let WAREHOUSE_ID = this.formField.WAREHOUSE_ID;
        this.formField.WAREHOUSE_ID = "";
        request({
          url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
          method: "post",
          data: {
            IS_ENABLE: "1"
          }
        }).then(res => {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_ID = WAREHOUSE_ID;
        });

        request({
          url: "/mp/wm/vl/warehouse/transfrom/queryTransfromTypeBox.do",
          method: "post",
          data: {}
        }).then(res => {
          res.listRow.map(item => {
            this.tableComponents[3].options.push({
              label: item.TRANSFORM_TYPE_NAME,
              value: item.TRANSFORM_TYPE
            });
          });
        });
      }
    },
    //保存
    save() {
      this.$utils.validataMoth(this, "validpopup");
      if (this.valid) {
        var saveObj = JSON.parse(JSON.stringify(this.formField));
        //保存表单
        this.saveForm(saveObj);
        storage.remove(this.$route.query.pageCode);
      }
    }
  }
};
</script>
<style type="text/css" scoped>
.filter-params .el-col label .curName {
  width: 150px;
}
</style>